﻿using shenghua.Basics;
using shenghua.Basics.ShippingTemplates;
using shenghua.Common.CustomValueType;
using shenghua.Member.ShippingAddresses;
using shenghua.Member.Coupons;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.UI;

namespace shenghua.Order.Orders.Median
{
    public class OrderCore
    {
        public Member.Members.Member Member { get; private set; }
        public SettingManager SettingManager { get; private set; }
        public CommodityCollection CommodityCollection { get; private set; }
        public MemberShippingAddress DeliveryAddress { get; private set; }
        public ShippingTemplate ShippingTemplate { get; private set; }
        public PaymentType PaymentType { get; private set; }

        /// <summary>
        /// 判定是否可以提交，比如为提供货运地址，则该值为false
        /// </summary>
        public bool CanSubmit
        {
            get
            {
                return ErrorMessages.Count == 0;
            }
        }
        public List<string> ErrorMessages { get; set; }

        public bool IsFreeShipping
        {
            get
            {
                if (ShippingTemplate == null || DeliveryAddress == null) return false;
                return ShippingTemplate.CanFreeShipping(DeliveryAddress.AreaTag, CommodityCollection.TotalCount, CommodityCollection.TotalAmount);
            }
        }

        public Money Freight
        {
            get
            {
                if (ShippingTemplate == null || DeliveryAddress == null) return new Money();

                if (IsFreeShipping)
                {
                    return new Money(0);
                }
                else
                {
                    return ShippingTemplate.GetFreight(DeliveryAddress.AreaTag, CommodityCollection.TotalCount, CommodityCollection.TotalAmount);
                }
            }
        }

        public Money TotalAmount
        {
            get
            {
                return CommodityCollection.TotalAmount + Freight;
            }
        }

        public string Subject
        {
            get
            {
                string subject = "";
                foreach(var item in CommodityCollection.Items)
                {
                    subject += $"[{item.Commodity.CommodityName}]";
                }
                if (subject.Length > 64) subject = subject.Substring(0, 64);
                return subject;
            }
        }


        public OrderCore(
            Member.Members.Member member,
            SettingManager settingManager,
            List<CommodityCollectionItem> items,
            MemberShippingAddress deliveryAddress,
            ShippingTemplate shippingTemplate,
            PaymentType paymentType,
            MemberCoupon coupon = null,
            int xpValue = 0)
        {
            ErrorMessages = new List<string>();

            Member = member;
            SettingManager = settingManager;
            DeliveryAddress = deliveryAddress;
            PaymentType = paymentType;
            ShippingTemplate = shippingTemplate;
            CommodityCollection = new CommodityCollection(items, coupon, xpValue, member, settingManager);
            
            if(deliveryAddress == null)
            {
                ErrorMessages.Add("必须提供收货地址");
                //throw new UserFriendlyException("必须提供收货地址");
            }
            else
            {
                if (deliveryAddress.MemberId != member.Id)
                {
                    throw new UserFriendlyException("收货地址不属于指定用户");
                }
            }

            if(shippingTemplate == null)
            {
                ErrorMessages.Add("必须选择货运方式");
                //throw new UserFriendlyException("必须选择收费模板");
            }
            else
            {
                if (!shippingTemplate.Enabled)
                {
                    throw new UserFriendlyException("运费模板未启用");
                }
                if (ShippingTemplate.TemplateType != settingManager.GetShippingTemplateType())
                {
                    throw new UserFriendlyException("无效的运费模板类型");
                }
            }
        }
    }
}
